package com.qsmx.store.app.main.suit;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.entity.suit.SuitTypeEntity;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.adapter.TabPagerAdapter;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import butterknife.BindView;

public class SuitListDelegate extends AtmosDelegate {

    private SuitTypeEntity mSuitTypeEntity;
    String[] titles;
    List<Fragment> fragmentList = new ArrayList<>();

    @BindView(R2.id.tl_suit)
    TabLayout tlSuit;
    @BindView(R2.id.vp_suit)
    ViewPager vpSuit;

    @Override
    public Object setLayout() {
        return R.layout.delegate_suit_home;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        initSuitTabData();

    }

    private void initSuitTabData() {

        HttpHelper.RestClientGet(new WeakHashMap<String, Object>(), HttpUrl.COMBO,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {
                        Type type = new TypeToken<SuitTypeEntity>() {
                        }.getType();
                        Gson gson = new Gson();
                        mSuitTypeEntity = gson.fromJson(response, type);
                        titles = new String[mSuitTypeEntity.getContent().size()];
                        for (int i = 0; i < mSuitTypeEntity.getContent().size(); i++) {
                            titles[i] = mSuitTypeEntity.getContent().get(i).getTitle();
                            SuitChildDelegate delegate = SuitChildDelegate.create(mSuitTypeEntity.getContent().get(i).getColCount(), mSuitTypeEntity.getContent().get(i).getId());
                            fragmentList.add(delegate);
                        }
                        initTab();
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });

    }

    private void initTab() {

        TabPagerAdapter tabPagerAdapter = new TabPagerAdapter(getChildFragmentManager(), fragmentList, titles);
        vpSuit.setAdapter(tabPagerAdapter);
        vpSuit.setOffscreenPageLimit(0);
        tlSuit.setupWithViewPager(vpSuit);
    }

}
